package com.springboot.system.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.system.domain.auth.Role;
import com.springboot.system.domain.auth.RoleVO;
import com.springboot.system.service.RoleService;
import com.springboot.core.log.annotation.Log;
import com.springboot.core.security.permission.CheckPermission;
import com.springboot.core.web.mvc.BaseRest;
import com.springboot.core.web.mvc.R;

/**
 * 角色接口
 * 
 * @ClassName: RoleRest
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 252956
 * @date 2019年10月21日 下午4:56:02
 *
 */
@RestController
@RequestMapping(value = "/rest/role")
public class RoleRest extends BaseRest {

	@Autowired
	private RoleService roleService;

	@CheckPermission("role:list")
	@RequestMapping(value = "page", method = RequestMethod.POST)
	public R page(@RequestBody RoleVO vo) {
		return R.ok(roleService.page(vo));

	}

	@RequestMapping(value = "all")
	public R all() {
		return R.ok(roleService.selectAll());
	}

	/**
	 * 添加角色
	 * 
	 * @param role
	 * @return
	 */
	@CheckPermission("role:add")
	@Log(value = "新增角色")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public R add(@RequestBody Role role) {
		roleService.add(role);
		return R.ok();
	}

	/**
	 * 修改角色
	 * 
	 * @param role
	 * @return
	 */
	@CheckPermission("role:edit")
	@Log(value = "修改角色")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public R update(@RequestBody Role role) {
		roleService.update(role);
		return R.ok();
	}

	/**
	 * 移除角色
	 * 
	 * @param id
	 * @return
	 */
	@CheckPermission("role:del")
	@Log(value = "删除角色")
	@RequestMapping(value = "delete")
	public R delete(@RequestParam("id") Integer id) {
		roleService.delete(id);
		return R.ok();
	}

	/**
	 * 分配角色对应的数据权限
	 * 
	 * @param roleRelationRightVO
	 * @return
	 */
	@CheckPermission("role:datascope")
	@Log(value = "角色分配数据权限")
	@RequestMapping(value = "saveDataScope", method = RequestMethod.POST)
	public R saveDataScope(@RequestBody Role role) {
		roleService.saveRelationDataScope(role);
		return R.ok();
	}

	@RequestMapping(value = "queryDataScope")
	public R queryDataScope(@RequestParam("id") Integer roleId) {
		return R.ok(roleService.selectDataScope(roleId));
	}

	@Log(value = "更新角色状态")
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	public R updateStatus(@RequestBody RoleVO vo) {
		roleService.updateStatus(vo);
		return R.ok();

	}

}
